
# 五子棋

# 编程思路
# 考虑数据结构
# 考虑流程（大流程  -》 小流程)

# 具体内容
# 棋盘 N*N
# 黑棋下棋
# 判断一下有没有连续5个黑
# 白旗下旗
# 判断一下有没有连续5个白

# line = [0 for i in range(10)]      # 这里的推导式表示，用for 循环 开辟0-9这十个位置 ，再将0这个元素填充在每个位置上
# print(line)
# chess = [[0 for i in range(10)] for j in range(10)] # 用for 循环 开辟0-9这十个位置，杂再将[0 for i in range(10)]这个元素填充在每个位置上
# print(chess)


class Gobang():

    # 初始化定义 （棋盘大小、棋盘信息、棋盘位置状态显示信息、确定先手）
    def __init__(self):
        # 棋盘大小
        self.N = 10
        # 棋盘初始信息
        self.chess = [[0 for i in range(self.N)] for j in range(self.N)]
        # 棋盘位置状态显示信息
        self.display = ['+','2','1']
        # 定义黑棋先下
        self.curBlack = True

    # 打印棋盘
    def printchess(self):
        for i in range(self.N):
            for j in range(self.N):
                print(self.display[self.chess[j][i]],end=' ')     # 这里有一个数据对应关系 行列关系对应
            print()

    # 主流程
    def play(self):
        # 用一个死循环来控制下棋不断进行
        while True:
            # 1. 打印棋盘
            self.printchess()
            # 2. 玩游戏
            self.playgame()
            # 3。 检测输赢
            if self.checkWin():
                print('赢了')
                self.printchess()
                break
            # 切换玩家
            self.curBlack = not self.curBlack

    # 玩家下棋
    def playgame(self):
        if self.curBlack:
            print('请黑方下棋')
        else:
            print('请白方下棋')
        while True:
            # 玩家输入下棋的坐标
            x = int(input('请输入下棋位置的横坐标：'))
            y = int(input('请输入下棋位置的纵坐标：'))
            if self.chess[y-1][x-1] == 0:
                if self.curBlack:
                    self.chess[y-1][x-1] = 2
                else:
                    self.chess[y-1][x-1] = 1
                break
            else:
                print('该处已有棋子，请重新输入棋子坐标')

    # 检测输赢
    def checkWin(self):
        if self.checkH():
            return True
        if self.checkV():
            return True
        if self.checkL():
            return True
        if self.checkR():
            return True
        return False

    def checkH(self):
        for i in range(self.N):
            for j in range(self.N):
                if self.chess[j][i]!=0 and self.chess[j][i] == self.chess[j+1][i]and self.chess[j][i] == self.chess[j+2][i]and self.chess[j][i] == self.chess[j+3][i]and self.chess[j][i] == self.chess[j+4][i]:
                    return True

    def checkV(self):
        for i in range(self.N):
            for j in range(self.N):
                if self.chess[j][i] != 0 and self.chess[j][i] == self.chess[j ][i+1] and self.chess[j][i] == \
                        self.chess[j][i+2] and self.chess[j][i] == self.chess[j][i+3] and self.chess[j][i] == \
                        self.chess[j][i+4]:
                    return True

    def checkL(self):
        for j in range(self.N - 4):
            # 每一行中判断的次数
            for i in range(self.N - 4):
                if self.chess[i][j] != 0 and self.chess[i][j] == self.chess[i + 1][j + 1] and self.chess[i][j] == \
                        self.chess[i + 2][j + 2] and self.chess[i][j] == self.chess[i + 3][j + 3] and self.chess[i][
                    j] == self.chess[i + 4][j + 4]:
                    return True

    def checkR(self):
        for j in range(4,self.N):
            # 每一行中判断的次数
            for i in range(4,self.N):
                if self.chess[i][j] != 0 and self.chess[i][j] == self.chess[i - 1][j - 1] and self.chess[i][j] == \
                        self.chess[i - 2][j - 2] and self.chess[i][j] == self.chess[i - 3][j - 3] and self.chess[i][
                    j] == self.chess[i - 4][j - 4]:
                    return True



game = Gobang()
game.play()
